package cn.youyeku.zsxh.ui.practice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.Utils.widgetsUtils;
import cn.youyeku.zsxh.bean.Opt;
import cn.youyeku.zsxh.bean.Question;


public class QuestionFragment extends Fragment implements View.OnClickListener,UpdateFragment{

    private int page;
    private View view;
    private RecyclerView mRecycleView;
    private SingleAdapter mSimpleAdapter;
    LinearLayoutManager layoutManager;
    LocalBroadcastManager mLocalBroadcastManager;


    private Boolean merrorSet=false;
    //单个问题合集
    protected Question question;
    protected String mAnswer;
    //转换成实际的代码
    List<Opt> opts = new ArrayList<>();
    private TextView mQuestionTvDescTitle;
    /**
     * 名称
     */
    private TextView mTvPaperName;
    /**
     * 1
     */
    private TextView mTvSequence;
    /**
     * /15
     */
    private TextView mTvTotalCount;
    private TextView mQuestionTvAnswerParse;

    public static QuestionFragment newInstance(int data) {

        LogUtils.w("newInstance");
        Bundle args = new Bundle();
        args.putInt("page", data);
        QuestionFragment fragment = new QuestionFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.w("onCreateView");
        view = inflater.inflate(R.layout.fgm_question, container, false);
        Bundle bundle = getArguments();
        if (bundle != null) {
            page = bundle.getInt("page");
            LogUtils.w(page + "page");
            question = QuestionPracticeActivity.questions.get(page);
            mAnswer = question.getAnwser();
            initView();

            layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecycleView.setLayoutManager(layoutManager);
            mSimpleAdapter = new SingleAdapter<Opt>(R.layout.item_opt) {

                @Override
                protected void onBindData(ListHolder listHolder, View item, final Opt data, final int pos) {
                    final TextView tv = listHolder.getChildView(R.id.item_pto_content);
                    final CheckBox opt = listHolder.getChildView(R.id.opt);


                    tv.setText(data.getContent());
                    opt.setText(data.getOpt());

                    widgetsUtils.toggleCheckBoxAndText(opt, data.getSelected());





                    if(QuestionPracticeActivity.submit){

                        //如果答案是错误的
                        if(lastPos==pos && !QuestionPracticeActivity.check_question.get(page)){
                            //设置后不重复设置
                            opt.setBackgroundResource(R.drawable.opt_round_selected_error);

                            return;
                        }

                    }


                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if(QuestionPracticeActivity.submit){

                                return;
                            }

                            if (lastPos == -1) {

                                lastPos = pos;

                                widgetsUtils.toggleCheckBoxAndText(opt, data.setSelected(true));

                            } else if (lastPos != pos) {

                                //取消前面的选项
                                mDatas.get(lastPos).setSelected(false);
                                notifyItemChanged(lastPos);

                                //选择后面的选项
                                widgetsUtils.toggleCheckBoxAndText(opt, data.setSelected(true));
                                lastPos = pos;

                            }
                            //实现点击后的事件 跳转到下一页面

                            //添加到数组
                            QuestionPracticeActivity.check_question.append(page, opt.getText().equals(mAnswer));
                            QuestionPracticeActivity.do_questions[page] = 1;


                            Intent intent = new Intent("youyeku.jumptonext");
                            mLocalBroadcastManager.sendBroadcast(intent);

                        }


                    });

                }
            };

            //
            opts.add(new Opt("A", question.getOp_a(), false));
            opts.add(new Opt("B", question.getOp_b(), false));
            opts.add(new Opt("C", question.getOp_c(), false));
            opts.add(new Opt("D", question.getOp_d(), false));

            mSimpleAdapter.add(opts);

            mRecycleView.setAdapter(mSimpleAdapter);

            //设置标题描述 高亮处理
            SpannableStringBuilder ssb = new SpannableStringBuilder("(单选题)");
            ssb.setSpan(new ForegroundColorSpan(Color.parseColor("#4abfed")), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.append(question.getDesc());
            mQuestionTvDescTitle.setText(ssb);
            mTvSequence.setText(page + 1 + "");
            mTvTotalCount.setText("/" + QuestionPracticeActivity.question_size);
            mQuestionTvAnswerParse.setText("答案解析： 选"+question.getAnwser()+" ,"+question.getAnswer_parse());
            mQuestionTvAnswerParse.setVisibility(View.GONE);


        }


        return view;
    }

    private void initView() {
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        mRecycleView = (RecyclerView) view.findViewById(R.id.question_recycleView);
        mQuestionTvDescTitle = (TextView) view.findViewById(R.id.question_tv_desc_title);
        mTvPaperName = (TextView) view.findViewById(R.id.tv_paper_name);
        mTvSequence = (TextView) view.findViewById(R.id.tv_sequence);
        mTvTotalCount = (TextView) view.findViewById(R.id.tv_total_count);
        mQuestionTvAnswerParse = (TextView) view.findViewById(R.id.question_tv_answer_parse);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }





    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.question_tv_desc_title:
                break;

        }
    }

    @Override
    public void update() {
        //显示答案
        mQuestionTvAnswerParse.setVisibility(View.VISIBLE);
        mSimpleAdapter.notifyDataSetChanged();
    }
}
